<?php
Class IOSS_Conf {
	private static $_configFileName = "Config.php";//配置文件的名字
	private static $_baseCfg = null;
	private static $_instance = null;
	
	private $_site_config = null;

	/** 取基础配置 */
	static function getConfig($key){
		if(self::$_baseCfg == null){
			require_once dirname(__FILE__).DIRECTORY_SEPARATOR.self::$_configFileName;
			self::$_baseCfg = $IOSS_Config;
		}
		if(array_key_exists($key, self::$_baseCfg)){
			return self::$_baseCfg[$key];
		}
		return null;
	}

	/** 日志存放目录 配置 */
	static function getLogPath(){
		$path = self::getConfig('log_path');
		if($path){
			return $path;
		}else{
			return dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR."application".DIRECTORY_SEPARATOR."logs".DIRECTORY_SEPARATOR;		
		}
	}
	
	/**
	 * 获取配置类实例
	 * @return IOSS_Conf
	 */
	static function getInstance(){
		if(self::$_instance == null){
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	private function __construct(){
		$cache = IOSS_Cache::getInstance();
		$cacheKey = __CLASS__.'::_siteCfg';
		$this->_site_config= $cache->get($cacheKey);
		if($this->_site_config=== false){
			$this->_site_config= Array();
			$db = IOSS_DB::getInstance();
			if($arr = $db->get_where('core_site_config')->result()){
				foreach ($arr as $row){
					$this->_site_config[$row->config_key] = $row->value;
				}
				$cache->set($cacheKey, $this->_site_config);
			}
		}
	}
	
	/**
	 * 获取一项配置 (core_site_confg 表配置项)
	 * @param string $key
	 * @return NULL
	 */
	public function item($key){
		return array_key_exists($key, $this->_site_config) ? $this->_site_config[$key] : null;
	}

	public function setItem($key, $value){
		$this->_site_config[$key] = $value;
	}
}
